const express = require('express')
const timeout = require("connect-timeout");
const consola = require('consola')
const { Nuxt, Builder } = require('nuxt')
const Rsp = require("../server/common/Rsp");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const initRouter = require("../server/router/index");
const initMiddleware = require("../server/middleware/index");



const app = express()




// Import and Set Nuxt.js options
const config = require('../nuxt.config.js')
const mysql = require("./plugins/mysql");

config.dev = process.env.NODE_ENV !== "production";


async function start() {

    mysql.initMysql();

    // Init Nuxt.js
    const nuxt = new Nuxt(config)

    const { host, port } = nuxt.options.server

    // Build only in dev mode
    if (config.dev) {
        const builder = new Builder(nuxt)
        await builder.build()
    } else {
        await nuxt.ready()
    }

    app.use(bodyParser.json({ limit: "10mb" }));
    app.use(
        bodyParser.urlencoded({
            extended: true
        })
    );

    app.use(cookieParser());


    // 初始化中间件
    initMiddleware(app);

    // 初始化路由
    initRouter(app);

    // Give nuxt middleware to express
    app.use(nuxt.render)

    app.use(timeout(10000));

    app.use((req, res, next) => {
        res.json(Rsp.build(Rsp.ACCESS_TIMEOUT));
    });

    app.use((err, req, res, next) => {
        consola.error(err);
        res.json(Rsp.build(Rsp.SERIOUS_ERROR));
    });

    // Listen the server
    app.listen(port, host)
    consola.ready({
        message: `Server listening on http://${host}:${port}`,
        badge: true
    })
}
start()
